<?php
/**
 * This file generates a RSS Feed for a given tool
 * The tool should be provided in the gVAR array as the variable gVAR['alias'] 
 */

require_once('../SiteIncludes.GLOBAL.php');

if(isset($gVAR['alias']) === true) {
    
    try {
	//header('Content-type: application/rss+xml; charset=ISO-8859-1');
	$Project =& $ProjectStash->stash($gVAR['alias']);
	$TemplateEngine->setTitle($Project->getName());
	$TemplateEngine->setHasSidebar(false);
	$TemplateEngine->setCentralContentFullWidth(true);
	$Project->instanciate();
	
	$announcements_title = "<a href=\"/tool/".$Project->getAlias()."\">".$Project->getName()."</a> Newsfeed";
	$announcements_url = "#";
	$announcements_feed_items = array();
	require_simplepie();
	$SimplePieA = new SimplePieAggregator($Project->getRSSfeeds());
	$SimplePieA->aggregate();
	$items =& array_splice($SimplePieA->get_items(), 0, 15);
	foreach($items as $key => $value) {
	    $date = $value->get_date();
	    $title = $value->get_title();
	    $url = $value->get_permalink();
	    $description = $value->get_description();
	    $icon = '/img/rss_large.png';//$value->getIconURL();
	    $announcements_feed_items[] = $Templater->makecontentfeeditem($date, $title, $url, $description, $icon);
	}
	$rss_url = $_ECLIPSEMDE['BASE_URL'].'/tool/'.$Project->getAlias().'/newsfeed.rss';
	if(count($items) === 0) {
	    $rss_html = '<a href="'.$rss_url.'"><img src="/img/rss2.gif" style="width: 20px; height: 15px;" alt="RSS Feed" /></a>';
	    $TemplateEngine->create_content_html($Templater->maincontent_feed($announcements_title, $announcements_url, $announcements_feed_items, $rss_html));
	    $TemplateEngine->create_content_html('<center><i>No news items to display for this tool</i>.</center>');
	} else {
	    $rss_html = '<a href="'.$rss_url.'"><img src="/img/rss2.gif" style="width: 16px; height: 16px;" alt="RSS Feed" /></a>';
	    $TemplateEngine->create_content_html($Templater->maincontent_feed($announcements_title, $announcements_url, $announcements_feed_items, $rss_html));
	    //$TemplateEngine->create_content_html('<center></center>');
	}
	
	$TemplateEngine->render();
	die();
    } catch(InvalidProjectException $e) {
	/* 404 */
    } catch(Exception $e) {
	$gVAR['e'] = 500;
	require_once($_ECLIPSEMDE['ROOT_WD'].'www-root/error_handler.php');
	die();
    }
} else {
    // 404
}

    // let the catch and else blocks fall through to the 404 
    $gVAR['e'] = 404;
    require_once($_ECLIPSEMDE['ROOT_WD'].'www-root/error_handler.php');
    die();
?>
